# -*- coding: utf-8 -*-
# @Author: QX
# @File: conftest.py
from time import sleep

import allure
import pytest

from allure import attach, attachment_type
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait

from examples.context import Context


@pytest.fixture(scope='function')
def get_readhomedriver(request):
    with allure.step('#登录成功共公部分'):
        dis={
          "platformName": "Android",
          "appium:platformVersion": "9",
          "appium:appPackage": "com.tal.kaoyan",
          "appium:appActivity": ".ui.activity.SplashActivity",
          "appium:deviceName": "emulator-5554",
          "appium:automationName": "UIAutomator2",
          "appium:noReset": False,
          "appium:newCommandTimeout": "3000"
        }
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',dis)
        sleep(3)
        #1.单击同意==id==com.tal.kaoyan:id/tip_commit
        driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/tip_commit').click()
        sleep(3)

        #2.单击我知道id==com.tal.kaoyan:id/tv_ok
        driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/tv_ok').click()
        sleep(3)

        #3.单击密码登录id==com.tal.kaoyan:id/loginRegistorcodeAndPassword
        pwlg = (AppiumBy.ID,'com.tal.kaoyan:id/loginRegistorcodeAndPassword')
        ele = WebDriverWait(driver,30,2).until(lambda x:x.find_element(*pwlg))
        ele.click()

        #driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/loginRegistorcodeAndPassword').click()
        sleep(3)
        yield driver
        def end():
            driver.press_keycode(3)
        request.addfinalizer(end)

@pytest.fixture(scope='session')
def get_readhomedriver1(request):
    with allure.step('#登录成功共公部分'):
        dis={
          "platformName": "Android",
          "appium:platformVersion": "9",
          "appium:appPackage": "com.tal.kaoyan",
          "appium:appActivity": ".ui.activity.SplashActivity",
          "appium:deviceName": "emulator-5554",
          "appium:automationName": "UIAutomator2",
          "appium:noReset": False,
          "appium:newCommandTimeout": "3000"
        }
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',dis)
        sleep(3)
        #1.单击同意==id==com.tal.kaoyan:id/tip_commit
        driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/tip_commit').click()
        sleep(3)

        #2.单击我知道id==com.tal.kaoyan:id/tv_ok
        driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/tv_ok').click()
        sleep(3)

        #3.单击密码登录id==com.tal.kaoyan:id/loginRegistorcodeAndPassword
        pwlg = (AppiumBy.ID,'com.tal.kaoyan:id/loginRegistorcodeAndPassword')
        ele = WebDriverWait(driver,30,2).until(lambda x:x.find_element(*pwlg))
        ele.click()

        #driver.find_element(AppiumBy.ID,'com.tal.kaoyan:id/loginRegistorcodeAndPassword').click()
        sleep(3)
        yield driver
        def end():
            driver.press_keycode(3)
        request.addfinalizer(end)

#钩子函数
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # 获得⽤例执⾏结果对象
    outcome = yield
    rep = outcome.get_result()
    # 判断⽤例调⽤阶段失败时
    if rep.when == "call" and rep.outcome != "passed":
        # 从Context对象中获取driver
        driver = getattr(Context, "driver")
        # 对当前屏幕截图
        screenshot = driver.get_screenshot_as_png()
        attach(screenshot, name="异常截图",   attachment_type=attachment_type.PNG)


@pytest.fixture(scope='function')
def get_readhdriver(request):
    dic = {
      "platformName": "Android",
      "appium:platformVersion": "9",
      "appium:appPackage": "com.zhao.myreader",
      "appium:appActivity": ".ui.home.MainActivity",
      "appium:deviceName": "emulator-5554",
      "appium:automationName": "UIAutomator2",
      "appium:noReset": False
    }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', dic)
    driver.implicitly_wait(20)
    sleep(2)
    yield driver
    def end():
        driver.press_keycode(3)

    request.addfinalizer(end)